package com.zunkye.provider.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.zunkye.domain.User;
import com.zunkye.resp.ProcessResp;
import com.zunkye.service.UserService;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

@Service(protocol = "dubbo")
public class UserServiceImpl implements UserService {
    private Map<Long, User> repository = new TreeMap<Long, User>();
    private Long idSequence = 1L;

    public ProcessResp addUser(User user) {
        user.setId(idSequence++);
        repository.put(user.getId(), user);

        ProcessResp resp = new ProcessResp();
        resp.setSuccess(true);
        resp.setMessage("添加成功！");
        return resp;
    }

    public User getUser(Long id) {
        return repository.get(id);
    }

    public List<User> getUsers() {
        return new ArrayList<User>(repository.values());
    }

    public ProcessResp removeUser(Long id) {
        repository.remove(id);

        ProcessResp resp = new ProcessResp();
        resp.setSuccess(true);
        resp.setMessage("删除成功！");
        return resp;
    }
}
